package cn.anycase.demo.config;

import javax.sql.DataSource;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;

import com.baomidou.mybatisplus.MybatisConfiguration;
import com.baomidou.mybatisplus.MybatisXMLLanguageDriver;
import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;

/**
 * <p>
 * </p>
 *
 * @author yuxiaobin
 * @date 2018/2/6
 */
@Configuration
public class MybatisPlusConfig implements EnvironmentAware {

    Environment env;

    @Bean("primary")
    @Primary
    public DataSource dataSource() throws ClassNotFoundException {
        return DataSourceBuilder.create()
                .url(env.getProperty("spring.datasource.url"))
                .type((Class<? extends DataSource>) Class.forName(env.getProperty("spring.datasource.type")))
                .driverClassName(env.getProperty("spring.datasource.driverClassName"))
                .username(env.getProperty("spring.datasource.username"))
                .password(env.getProperty("spring.datasource.password"))
                .build();
    }

    @Bean("mainsf")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("primary") DataSource dataSource,
                                               GlobalConfiguration globalConfiguration
    ) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        configuration.setMapUnderscoreToCamelCase(true);
        sqlSessionFactory.setConfiguration(configuration);
        Interceptor[] plugins;
        plugins = new Interceptor[]{new PaginationInterceptor()};
        sqlSessionFactory.setPlugins(plugins);
        sqlSessionFactory.setGlobalConfig(globalConfiguration);
        return sqlSessionFactory.getObject();
    }

    @Bean
    public GlobalConfiguration globalConfiguration() {
        GlobalConfiguration conf = new GlobalConfiguration();
        conf.setIdType(1);
        return conf;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer();
//        scannerConfigurer.setBasePackage("com.roadshare.drbox.*.mapper,com.roadshare.drbox.*.*.mapper,");
        scannerConfigurer.setBasePackage("cn.anycase.demo.mapper;cn.anycase.demo.*.mapper;");
        scannerConfigurer.setSqlSessionFactoryBeanName("mainsf");
        return scannerConfigurer;
    }


    @Override
    public void setEnvironment(Environment environment) {
        this.env = environment;
    }
}
